package com.iknight.theory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.theory.api.TeLineService;
import com.iknight.theory.mapper.TeLineMapper;
import com.iknight.theory.model.TELineModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeLineServiceImpl implements TeLineService<TELineModel, String> {
    private static final Logger logger = LogManager.getLogger(TeLineServiceImpl.class);

    @Autowired
    private TeLineMapper<TELineModel, String> teLineMapper;

    @Override
    public void save(TELineModel record) {
        if (null == teLineMapper.selectOne(record.getLine_id())) {
            teLineMapper.insert(record);
            logger.info("执行插入成功!");
        } else {
            teLineMapper.update(record);
            logger.info("执行更新成功!");
        }
    }

    @Override
    public void delete(String pkey) {
        teLineMapper.delete(pkey);
    }

    @Override
    public TELineModel selectOne(String pkey) {
        TELineModel record = teLineMapper.selectOne(pkey);
        return record;
    }

    @Override
    public PageInfo<TELineModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        if (pageSize == 0) {
            List<TELineModel> result = teLineMapper.selectByCond(params);
            return new PageInfo<>(result);
        } else {
            PageHelper.startPage(pageStart, pageSize, true);
            List<TELineModel> result = teLineMapper.selectByCond(params);

            PageInfo<TELineModel> page = new PageInfo<>(result);
            logger.info("分页查询成功,当前查询页数为:" + pageStart + ",单页条数为:" + pageSize);
            return page;
        }

    }

    @Override
    public Object getLineInfoByTranId(String tranId) {
        return teLineMapper.getLineInfoByTranId(tranId);
    }
}
